﻿using Elsa.Extensions;
using Elsa.Workflows.Memory;
using T.FAS.Runtime.MF.Entity;
using T.FAS.Runtime.MF.SPI;

namespace T.FAS.Runtime.MF.Activities.Helper
{
    internal class VariableHelper
    {
        /// <summary>
        /// 生成变量信息
        /// </summary>
        /// <param name="item"></param>
        /// <param name="activityId"></param>
        /// <param name="needAppendActivityId"></param>
        /// <returns></returns>
        public static Variable GenerateVariable(UserVariable item)
        {
            var varName = string.IsNullOrEmpty(item.CompositeVarName) ? item.VariableName : item.CompositeVarName;
            var variable = new Variable();
            if (string.IsNullOrEmpty(item.Type))
            {
                item.Type = "object";
            }
            if (varName.EndsWith(MFConst.IterationResultVariableSuffix))
            {
                variable = new Variable<object[]>(varName,null);
                variable.WithWorkflowStorage();
                return variable;
            }
            switch (item.Type.ToLower())
            {
                case "string":
                case "guid": // GUID按字符串处理
                case "text":
                    { variable = new Variable<string>(varName, ""); break; }
                case "bool":
                case "boolean":
                    {
                        variable = new Variable<bool>(varName, false); break;
                    }
                case "int":
                case "int32":
                    {
                        variable = new Variable<int>(varName, 0); break;
                    }
                case "long":
                    {
                        variable = new Variable<long>(varName, 0L); break;
                    }
                case "single":
                    {
                        variable = new Variable<float>(varName, 0f); // Single即float
                        break;
                    }
                case "double":
                    {
                        variable = new Variable<double>(varName, 0d); break;
                    }
                case "decimal":
                    {
                        variable = new Variable<decimal>(varName, 0m); break;
                    }
                case "datetimeoffset":
                    {
                        variable = new Variable<DateTimeOffset>(varName, default); break;
                    }
                case "timespan":
                    {
                        variable = new Variable<TimeSpan>(varName, default); break;
                    }
                default:
                    {
                        variable = new Variable<object>(varName, null); // 未知类型
                        break;
                    }
            }

            if (variable != null)
            {
                variable.WithWorkflowStorage();
                return variable;
            }
            return null;
            //else if (!string.IsNullOrEmpty(item.SourceNode) && !string.IsNullOrEmpty(item.SourceVariable))
            //{

            //}
        }
        public static string ConvertVariableValue(string dataType, string VariableName)
        {
            var result = VariableName;

            if (dataType == "datetime")
            {
                result = $"new Date({VariableName}).getTime()";
            }
            else if (dataType == "time")
            {
                result = $"new Date('2000-01-01 {VariableName}').getTime()";
            }
            return result;
        }
        public static string ConvertConstantValue(string dataType, string constantValue)
        {
            var result = constantValue;
            if (dataType == "string" || dataType == "textInput")
            {
                result = $"'{constantValue}'";
            }
            else if (dataType == "datetime")
            {
                result = $"new Date('{constantValue}')";
            }
            else if (dataType == "time")
            {
                result = $"new Date('2000-01-01 {constantValue}')";
            }
            return result;
        }
    }
}